package net.atomarrow.pr.util;

import java.util.List;

import javax.servlet.http.HttpSession;

import net.atomarrow.ioc.ActionContext;
import net.atomarrow.pr.bean.PermissionBean;
import net.atomarrow.pr.domain.TbRole;
import net.atomarrow.pr.staticvariable.PermissionSv;

public class PermissionCheckUtil {

    public static List<TbRole> getCurrentAccountRoles(){
        HttpSession session = ActionContext.getContext().getSession();
        return (List<TbRole>) session.getAttribute(PermissionSv.SESSION_KEY_PERMISSION_ROLE);
    }
    
    public static boolean checkRoleExist(int roleId){
        for(TbRole role:getCurrentAccountRoles()){
            if(role.getId()==roleId){
                return true;
            }
        }
        return false;
    }

    public static boolean checkUrl(String url) {
        HttpSession session = ActionContext.getContext().getSession();
        PermissionBean permissionBean =  (PermissionBean) session.getAttribute(PermissionSv.SESSION_KEY_PERMISSION_BEAN);
        if(permissionBean==null){
            return false;
        }
        return permissionBean.check(url);
    }
}
